package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.Like;
import com.tanhua.domain.mongo.Visitor;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.LikeApi;
import com.tanhua.dubbo.api.RecommendUserApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LikeApiImpl implements LikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RecommendUserApi recommendUserApi;

    @Override
    public PageResult queryByPage(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.limit(pagesize).skip((page - 1)*pagesize);
        List<Like> likes = mongoTemplate.find(query, Like.class);
        long count = mongoTemplate.count(query, Like.class);
        return new PageResult(page,pagesize,(int)count,likes);
    }

    @Override
    public Long countFans(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        long count = mongoTemplate.count(query, Like.class) + countBoth(userId);
        return count;
    }

    @Override
    public Long countLove(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Like.class);
        return count;
    }

    @Override
    public Long countBoth(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Friend.class);
        return count;
    }

    @Override
    //互关列表
    public PageResult queryLikeBothByPage(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.limit(pagesize).skip((page - 1)*pagesize);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);

        List<Map<String,Object>> result = new ArrayList<>();
        if(friends != null){
            for (Friend friend : friends) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId",friend.getFriendId());
                map.put("score", recommendUserApi.queryScore(friend.getUserId(),friend.getFriendId()));
                result.add(map);
            }
        }

        return new PageResult(page,pagesize,(int)count,result);
    }

    @Override
    //粉丝列表
    public PageResult queryFansByPage(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.limit(pagesize).skip((page - 1)*pagesize);
        List<Like> likes = mongoTemplate.find(query, Like.class);
        long count = mongoTemplate.count(query, Like.class);

        // 统一返回一个map，存储用户id、缘分值。在通过List封装map
        List<Map<String,Object>> result = new ArrayList<>();
        // 封装返回结果
        if(likes != null){
            for (Like like : likes) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", like.getLikeUserId());
                map.put("score",recommendUserApi.queryScore(like.getUserId(),like.getLikeUserId()));
                result.add(map);
            }
        }

        PageResult pageResult = queryLikeBothByPage(userId, page, pagesize);
        List<Map<String,Object>> items = (List<Map<String, Object>>) pageResult.getItems();
        result.addAll(items);

        return new PageResult(page,pagesize,(int)count,result);
    }

    @Override
    //谁看了我
    public PageResult queryVisitorByPage(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("date")));
        query.limit(pagesize).skip((page - 1)*pagesize);
        List<Visitor> visitors = mongoTemplate.find(query, Visitor.class);
        long count = mongoTemplate.count(query, Visitor.class);

        // 统一返回一个map，存储用户id、缘分值。在通过List封装map
        List<Map<String,Object>> result = new ArrayList<>();
        // 封装返回结果
        if(visitors != null){
            for (Visitor visitor : visitors) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", visitor.getVisitorUserId());
                map.put("score",recommendUserApi.queryScore(visitor.getUserId(),visitor.getVisitorUserId()));
                result.add(map);
            }
        }

        return new PageResult(page,pagesize,(int)count,result);
    }

    @Override
    //喜欢列表
    public PageResult queryLoveByPage(Integer page, Integer pagesize, Long userId) {
        PageResult pageResult = queryByPage(page, pagesize, userId);
        List<Like> list = (List<Like>) pageResult.getItems();

        // 统一返回一个map，存储用户id、缘分值，在通过List封装map
        List<Map<String,Object>> result = new ArrayList<>();
        // 封装返回结果
        if(list != null){
            for (Like like : list) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("userId", like.getLikeUserId());
                map.put("score", recommendUserApi.queryScore(like.getUserId(), like.getLikeUserId()));

                result.add(map);
            }
        }
        pageResult.setItems(result);
        return pageResult;
    }

    @Override
    //删除粉丝数据
    public void remove(Long userId, Integer fansId) {
        Query query = new Query(Criteria.where("userId").is(fansId).and("likeUserId").is(userId));
        mongoTemplate.remove(query, Like.class);
    }

    @Override
    public void save(Integer friendId, Long userId) {
        Like like = new Like();
        like.setUserId(friendId.longValue());
        like.setLikeUserId(userId);
        like.setCreated(System.currentTimeMillis());
        mongoTemplate.save(like);
    }
}
